'use strict';

import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('ZhekouLimitStep1');
export const CREAT_ZHEKOU_START = ns('CREAT_ZHEKOU_START');
export const CREAT_ZHEKOU_ERR = ns('CREAT_ZHEKOU_ERR');
export const CREAT_ZHEKOU_DONE = ns('CREAT_ZHEKOU_DONE');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const CREATE_NEW = ns('CREATE_NEW');
export const ADD_ITEM_TO_ACTIVE = ns('ADD_ITEM_TO_ACTIVE');
export function closeDialog() {
	  return {type: CLOSE_DIALOG,visible:false};
	}
export function createNew(){
	return {type: CREATE_NEW,visible:false};
}
export function createZhekou(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: CREAT_ZHEKOU_START,
      data: params
    });
		//web环境执行创建折扣
		ajax({
			  api: 'createTaskLimitActive',
			  method: 'POST',
			  data:params
			},(json) => {
			  //成功后
				ajax({
					api:'saveLimitToDatabase',
					method:'GET',
					data:{...json,...params}
				},(json_save)=>{
					//保存到数据库成功
					  dispatch({
						type: CREAT_ZHEKOU_DONE,
						activityId: json.act_id,
						actionDone:true
					  });
				},(json_save)=>{
					//保存到数据库失败
					dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:json_save.msg,
						loginFalse:json_save.code==201
					});
				}
			  );

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  
			  dispatch({
				type: CREAT_ZHEKOU_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});

  };
}

export function addLimitItemToActive(itemset,activityId) {
	return (dispatch) => {
		//发送请求前
	    dispatch({
	        type: CREAT_ZHEKOU_START,
	        data: itemset
	    });
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : "taobao.ump.detail.add",
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:msg,
							loginFalse:false
						});
					}
				} );
		}else{
			
			const params = {
					activity_id : activityId,
					item_id:itemset.num_iid,
					promotion_value:itemset.promotion_value,
					promotion_type:itemset.promotion_type,
					is_limit_buy:true,
					limit_count:itemset.limit_buy_count,
					price:itemset.price
			}
			ajax({
			  api: "addLimitToRingActive",
			  method: 'GET',
			  data:params
			},(json) => {
				const newItemSet = {...itemset,status:"done",detail_id:json.detail_id};
				ajax({
				  api: "saveLimitItemToDatabase",
				  method: 'GET',
				  data:newItemSet
				},(data_json) => {
					dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet});
				},(data_json) =>{
					const login = (json.code==210);
					dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:data_json.msg+(data_json.sub_msg?data_json.sub_msg:""),
						loginFalse:login
					});
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: CREAT_ZHEKOU_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
